package quq.missq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;

import java.util.HashMap;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.MissQApplication;
import quq.missq.R;
import quq.missq.beans.UserBean;
import quq.missq.utils.MD5;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by ghost on 15/7/1.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, VolleyTool.HTTPListener {
    private EditText mPhone;
    private EditText mPassword;
    private TextView mForgetPassord;
    private RelativeLayout mRegister;
    private ImageView mBack;
    private RelativeLayout mLogin;
    private String phone,password = "";
    private int LOGIN_SUCCSS = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        initView();
        iniListener();
    }

    private void initView() {
        mPhone = (EditText) findViewById(R.id.phone);
        mPhone.setInputType(InputType.TYPE_CLASS_NUMBER);
        mPassword = (EditText) findViewById(R.id.password);
        mForgetPassord = (TextView)findViewById(R.id.forgetPassword);
        mRegister = (RelativeLayout)findViewById(R.id.register);
        mBack = (ImageView)findViewById(R.id.back);
        mLogin = (RelativeLayout)findViewById(R.id.login);
    }

    private void iniListener() {
        mForgetPassord.setOnClickListener(this);
        mRegister.setOnClickListener(this);
        mBack.setOnClickListener(this);
        mLogin.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            finish();
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.forgetPassword:
                intent = new Intent(this, ResetPasswordActivity1.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.register:
                intent = new Intent(this, RegisterActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.login:
                login();
                break;
        }
    }

    private void login(){
        phone = mPhone.getText().toString();
        password = mPassword.getText().toString();
        if (phone.isEmpty() || phone.length() != 11) {
            showToast("请输入正确的手机号");
            return;
        }
        if (password.isEmpty() || password.length() < 6) {
            showToast("请输入6到12位的密码");
            return;
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("password", MD5.getMD5(MD5.getMD5(password)));
        VolleyTool.post(this, Constants.LOGIN_URL, params, this, Constants.LOGIN, UserBean.class);

    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        UserBean b = (UserBean) t;
        if (b.getCode() >= 0) {
            final UserBean.User user = b.getData();
            user.setPassword(MD5.getMD5(MD5.getMD5(password)));
            UserTools.saveUser(this, user);
            /////////////////////////////////
            // 加个登陆试试
            EMChatManager.getInstance().login(user.getId()+"",MD5.getMD5(MD5.getMD5(password)),new EMCallBack() {
                @Override
                public void onSuccess() {
                    //  Toast.makeText(TaPageActivity.this, "chenggong" + MD5.getMD5(MD5.getMD5(user.getPassword())), Toast.LENGTH_SHORT).show();
                    // 登陆成功，保存用户名密码
                    MissQApplication.getInstance().setUserName(user.getId()+"");
                    MissQApplication.getInstance().setPassword(MD5.getMD5(MD5.getMD5(password)));

                    // 更新当前用户的nickname 此方法的作用是在ios离线推送时能够显示用户nick
                    boolean updatenick = EMChatManager.getInstance().updateCurrentUserNick(
                            user.getNkname());
                    Log.e("xxxxx","sssss");
                }

                @Override
                public void onError(int i, String s) {

                }

                @Override
                public void onProgress(int i, String s) {

                }
            });
            Intent intent = new Intent();
            intent.putExtra("user",user);
            setResult(LOGIN_SUCCSS, intent);
            finish();
            showToast("登录成功");
        }else {
            showToast(b.getMessage());
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }
}
